package com.taomu.app.zongchouhuli.moudle.login.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.taomu.app.zongchouhuli.MainActivity;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.CommonWebActivity;
import com.taomu.app.zongchouhuli.moudle.login.contract.LoginContract;
import com.taomu.app.zongchouhuli.moudle.login.contract.impl.LoginPresenter;
import com.taomu.app.zongchouhuli.utils.StorageHelper;
import com.taomu.app.zongchouhuli.weight.popwindow.PopSelectAccountManager;

import java.util.List;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity<LoginContract.Presenter> implements LoginContract.View {

    private EditText mAccountEt;

    private ImageView mMoreAccountIm;

    private EditText mPwdEt;

    private TextView mRegisterTv;

    private TextView mForgetTv;

    private CheckBox mProtocalCheck;

    private TextView mProtocalTv;

    private TextView mSubmitTv;

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_login;
    }

    @Override
    protected void initView() {
        mAccountEt = findViewById(R.id.login_user_et);
        mMoreAccountIm = findViewById(R.id.login_more_account);
        mPwdEt = findViewById(R.id.login_pwd_et);
        mRegisterTv = findViewById(R.id.login_register_tv);
        mForgetTv = findViewById(R.id.login_forget_tv);
        mProtocalCheck = findViewById(R.id.login_protocal_check);
        mProtocalTv = findViewById(R.id.login_protocal_tv);
        mSubmitTv = findViewById(R.id.login_submit_tv);
    }

    private boolean isNoLogin;

    @Override
    protected void initData() {
        isNoLogin = getIntent().getBooleanExtra("nologin", false);
       // mPresenter.isSubmitCanClick(mAccountEt,mPwdEt,mProtocalCheck);
    }

    @Override
    protected void initEvent() {
        mMoreAccountIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAccountPop();
            }
        });
        mRegisterTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        mForgetTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,ForgetActivity.class);
                startActivity(intent);
            }
        });
        mProtocalTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getProtocal();
            }
        });
        mSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(getAccount())){
                    showToast("请输入您的账号");
                    return;
                }
                if(TextUtils.isEmpty(getPwd())){
                    showToast("请输入您的密码");
                    return;
                }
                if(!mProtocalCheck.isChecked()){
                    showToast("请同意用《户服务协议》");
                    return;
                }
                addNewAccountHis();
                mPresenter.login(getAccount(),getPwd());
            }
        });
    }

    @Override
    protected LoginContract.Presenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void setSubmitCanClick(int bgcolor, boolean enable) {
        mSubmitTv.setBackgroundColor(getResources().getColor(bgcolor));
        mSubmitTv.setEnabled(enable);
    }

    @Override
    public void setLogin() {
        if(isNoLogin){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }else {
            showToast("登录成功");
        }
        finish();
    }

    @Override
    public void setArticleInfo(String msg) {
        CommonWebActivity.startWithContent(this,msg,"用户协议");
    }

    private PopSelectAccountManager popManager;

    private void showAccountPop(){
        if(StorageHelper.getAccountsHis().size() != 0) {
            if (popManager == null) {
                popManager = new PopSelectAccountManager(this);
            }
            popManager.setPopSelectAccountListener(new PopSelectAccountManager.PopSelectAccountListener() {
                @Override
                public void onItemClick(String entity) {
                    mAccountEt.setText(entity);
                }
            });
            popManager.setData(StorageHelper.getAccountsHis());
            popManager.showPopWindow(mAccountEt);
        }
    }

    private void addNewAccountHis(){
        List<String> accounts = StorageHelper.getAccountsHis();
        if(accounts.size() == 0){
            accounts.add(getAccount());
        }else{
            int index = -1;
            for(int i=0;i<accounts.size();i++){
                if(getAccount().equals(accounts.get(i))){
                    index = i;
                }
            }
            if(index != -1){
                accounts.remove(index);
            }
            accounts.add(0,getAccount());
        }
        StorageHelper.saveAccountHis(JSON.toJSONString(accounts));
    }

    private String getAccount(){
        return mAccountEt.getText().toString();
    }

    private String getPwd(){
        return mPwdEt.getText().toString();
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {

    }
}
